package com.rbcs.base.project.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.base.model.ProjectType;
import com.systop.core.ApplicationException;
import com.systop.core.service.BaseGenericsManager;

@Service
public class ProjectTypeManager extends BaseGenericsManager<ProjectType> {
	
	/**
	 * 保存项目类别，进行编号唯一性验证
	 */
	@Transactional
	public void save(ProjectType type){
		if (type != null){
			if (getDao().exists(type, "code")){
				throw new ApplicationException("项目分类编号已经存在：" + type.getCode());
			}else{
				getDao().saveOrUpdate(type);
			}
		}
	}

	public List<ProjectType> getTypes() {
		return query("from ProjectType t order by t.code");
	}

	/**
	 * 获得该类别包含的项目数量
	 * @param t
	 * @return
	 */
	public Long getProjectCounts(ProjectType t) {
		long count = 0;
		if (t != null) {
			String hql = "select count(p.id) from Project p where p.projectType.id = ?";
			count = (Long) getDao().findObject(hql, t.getId());
		}
		return count;
	}
}
